﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AliFox.BaseService.BaseData.DataDictionaryManagement.Dto;
using AliFox.BaseService.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace AliFox.BaseService.BaseData.DataDictionaryManagement;

[Authorize(BaseServicePermissions.DataDictionary.Default)]
public class DictionaryDetailAppService(
    IRepository<DataDictionaryDetail, Guid> detailRepository,
    IRepository<DataDictionary, Guid> masterRepository)
    : ApplicationService, IDictionaryDetailAppService
{
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PagedResultDto<DictionaryDetailDto>> GetAll(GetDictionaryDetailInputDto input)
    {
        var query = (await detailRepository.GetQueryableAsync()).Where(_ => _.Pid == input.Pid);
        var items = await query.OrderBy(_ => _.Sort)
            .Skip(input.SkipCount)
            .Take(input.MaxResultCount)
            .ToListAsync();

        var dots = ObjectMapper.Map<List<DataDictionaryDetail>, List<DictionaryDetailDto>>(items);
        var totalCount = await query.CountAsync();
        return new PagedResultDto<DictionaryDetailDto>(totalCount, dots);
    }

    /// <summary>
    /// 列表查询
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<ListResultDto<DictionaryDetailDto>> GetAllByDictionaryName(string name)
    {
        var master = await masterRepository.GetAsync(_ => _.Name == name);
        var details = await(await detailRepository.GetQueryableAsync()).Where(_ => _.Pid == master.Id)
            .OrderBy(_ => _.Sort)
            .ToListAsync();

        return new ListResultDto<DictionaryDetailDto>(ObjectMapper.Map<List<DataDictionaryDetail>, List<DictionaryDetailDto>>(details));
    }

    public async Task<DictionaryDetailDto> Get(Guid id)
    {
        var result = await detailRepository.GetAsync(id);
        return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(result);
    }

    [Authorize(BaseServicePermissions.DataDictionary.Create)]
    public async Task<DictionaryDetailDto> Create(CreateOrUpdateDictionaryDetailDto input)
    {
        var master = await masterRepository.FirstOrDefaultAsync(_ => _.Id == input.Pid);
        if(master == null)
        {
            throw new BusinessException("未找到字典！");
        }

        var exist = await detailRepository.FirstOrDefaultAsync(_ => _.Label == input.Label);
        if(exist != null)
        {
            throw new BusinessException("名称：" + input.Label + "字典已存在");
        }

        var res = await detailRepository.InsertAsync(new DataDictionaryDetail(
            GuidGenerator.Create(),
            input.Pid,
            input.Label,
            input.Value,
            input.Sort
            ));

        return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(res);
    }

    [Authorize(BaseServicePermissions.DataDictionary.Update)]
    public async Task<DictionaryDetailDto> Update(Guid id, CreateOrUpdateDictionaryDetailDto input)
    {
        var detail = await detailRepository.GetAsync(id);

        detail.Label = input.Label;
        detail.Value = input.Value;
        detail.Sort = input.Sort;

        await detailRepository.UpdateAsync(detail);

        return ObjectMapper.Map<DataDictionaryDetail, DictionaryDetailDto>(detail);
    }

    [Authorize(BaseServicePermissions.DataDictionary.Delete)]
    public async Task Delete(List<Guid> ids)
    {
        foreach (var id in ids)
        {
            await detailRepository.DeleteAsync(id);
        }
    }
}